<?php


namespace app\controller\api;


use app\BaseController;
use app\common\exception\ApiException;
use app\model\user\UserCouponModel;
use app\service\user\CouponService;
use app\service\user\UserCouponService;

class CouponController extends BaseController
{

    /**
     * 获取优惠券列表
     * @return array|\think\response\Json
     */
    public function getUserCouponList()
    {
        try {
            $userId = $this->request->middleware('userId');
            $where = [];
            $type = $this->request->param('type', 0, 'int');
            // 可用列表
            if ($type == 1) {
                $idArr = UserCouponModel::where('user_id', $userId)
                    ->where('is_used', 0)
                    ->where('status', 0)
                    ->where('use_end_time', '>=', time())
                    ->column('id');
                $where[] = ['id', 'in', $idArr];
            }
            // 已过期
            if ($type == 2) {
                $idArr = UserCouponModel::where('user_id', $userId)
                    ->where('is_used', 0)
                    ->where('use_end_time', '<', time())
                    ->column('id');
                $where[] = ['id', 'in', $idArr];
            }
            // 已使用
            if ($type == 3) {
                $idArr = UserCouponModel::where('user_id', $userId)
                    ->where('is_used', 1)
                    ->column('id');
                $where[] = ['id', 'in', $idArr];
            }
            $list = UserCouponModel::where($where)
                ->order('amount', 'desc')
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach ($list['data'] as &$v) {
                $v['use_end_time'] = date('Y-m-d H:i', $v['use_end_time']);
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 可领优惠券
     * @return \think\response\Json
     */
    public function getCouponList()
    {
        try {
            $userId = $this->request->middleware('userId');
            $userCouponService = new CouponService();
            $list = $userCouponService->getCouponList($userId);
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 领取优惠券
     * @return \think\response\Json
     */
    public function sendCouponToUser()
    {
        try {
            $userId = $this->request->middleware('userId');

            $userCouponService = new CouponService();
            $list = $userCouponService->getCouponList($userId);
            if (!$list) {
                throw new ApiException('没有待领取的优惠券');
            }
            $couponIdArr = array_column($list, 'id');

            $userCouponService = new UserCouponService();
            $userCouponService->sendCouponToUser($userId, $couponIdArr);
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }
}